﻿using AutoMapper;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.Common.NumberingCenter;

namespace PowerHub.MES.Services.Shopfloor
{
    /// <summary>
    /// 产线接口实现层
    /// </summary>
    public class Shopfloor_ProductionLineServices : BaseServices<Shopfloor_ProductionLine>, IShopfloor_ProductionLineServices
    {
        private IShopfloor_ProductionLineRepository _iShopfloor_ProductionLineRepository;
        private IShopfloor_WorkStationRepository _iShopfloor_WorkStationRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public Shopfloor_ProductionLineServices(PowerHubMesContext powerHubMesContext, IShopfloor_ProductionLineRepository iShopfloor_ProductionLineRepository, 
            UserServices userServices, IMapper mapper, IShopfloor_WorkStationRepository shopfloor_WorkStationRepository) : base(powerHubMesContext)
        {
            _iShopfloor_ProductionLineRepository = iShopfloor_ProductionLineRepository;
            _iShopfloor_WorkStationRepository = shopfloor_WorkStationRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取产线列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Shopfloor_ProductionLine>>> GetShopfloor_ProductionLinePageListAsync(QueryShopfloor_ProductionLineDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iShopfloor_ProductionLineRepository.GetShopfloor_ProductionLinePageListAsync(query);
        }

        /// <summary>
        /// 添加产线
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateShopfloor_ProductionLineAsync(Shopfloor_ProductionLineDTO query)
        {
            Shopfloor_ProductionLine ProductionLine = await GetEntityAsync(d => d.ProductionLineName.Equals(query.ProductionLineName)&& d.IsDel==0);
            if (ProductionLine!=null)
            {
                throw new Exception("你输入的产线名称已存在！");
            }
            //实体转换
            var Shopfloor_ProductionLine = _mapper.Map<Shopfloor_ProductionLine>(query);
            Shopfloor_ProductionLine.Id = Guid.NewGuid().ToString();
            Shopfloor_ProductionLine.ProductionLineNo = string.IsNullOrEmpty(query.ProductionLineNo) || query.ProductionLineNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.ProductionLineName) : query.ProductionLineNo;
            Shopfloor_ProductionLine.Creator = _userServices.UserName;

            return await _iShopfloor_ProductionLineRepository.CreateShopfloor_ProductionLineAsync(Shopfloor_ProductionLine);
        }


        /// <summary>
        /// 生成编号
        /// </summary>
        /// <param name="customCode"></param>
        /// <param name="sequenceLength"></param>
        /// <returns></returns>
        public string GetProductionLineNO(string customCode = "56", int sequenceLength = 5)
        {
            Shopfloor_ProductionLine shopfloor_ProductionLine = GetEntityListAsync(u => u.IsDel == 0).Result.OrderByDescending(u => u.ProductionLineNo).FirstOrDefault();

            int sequenceNumber = 0;
            if (shopfloor_ProductionLine != null)
            {
                string JobNumber = shopfloor_ProductionLine.ProductionLineNo.Substring(shopfloor_ProductionLine.ProductionLineNo.Length - 5);
                sequenceNumber = Convert.ToInt32(JobNumber);
            }
          
            // 获取当前年份的简写（只取后两位）  
            string yearShort = DateTime.Now.Year.ToString().Substring(2, 2);
            sequenceNumber++;
            string sequence = sequenceNumber.ToString().PadLeft(sequenceLength, '0');

            return yearShort + customCode + sequence;
        }

        /// <summary>
        /// 批量删除产线
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionShopfloor_ProductionLineAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iShopfloor_ProductionLineRepository.DeletionShopfloor_ProductionLineAsync(ids);
        }


        /// <summary>
        /// 更新产线
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateShopfloor_ProductionLineAsync(Shopfloor_ProductionLineDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取产线信息
            Shopfloor_ProductionLine Shopfloor_ProductionLine = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (Shopfloor_ProductionLine == null)
            {
                throw new Exception("更新产线信息发生了异常,请刷新页面后试试");
            }
            Shopfloor_ProductionLine.ProductionLineName = query.ProductionLineName;
            Shopfloor_ProductionLine.UserId = query.UserId;
            Shopfloor_ProductionLine.WorkshopId = query.WorkshopId;
            Shopfloor_ProductionLine.Area = query.Area;
            Shopfloor_ProductionLine.Updator = _userServices.UserName;


            return await _iShopfloor_ProductionLineRepository.UpdateShopfloor_ProductionLineAsync(Shopfloor_ProductionLine);

        }


        /// <summary>
        /// 根据产线Id获取产线工作站列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<OperationResult<List<Shopfloor_WorkStation>>> GetShopfloor_ProductionLineByIdWorkStationListAsync(QueryShopfloor_ProductionLineDTO pageQuery)
        {
            return await _iShopfloor_WorkStationRepository.GetShopfloor_ProductionLineByIdWorkStationListAsync(pageQuery);
        }
    }
}
